package cn.zd.demo1.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.zd.demo1.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MyDynamicFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyDynamicFragment extends Fragment {


    private MyDynamicFragment() {
    }

    public static MyDynamicFragment newInstance(int position, int img_id,String name, String detail) {
        MyDynamicFragment fragment = new MyDynamicFragment();
        Bundle arguments = fragment.getArguments();
        Bundle args = new Bundle();
        args.putInt("position", position);
        args.putInt("img_id", img_id);
        args.putString("name", name);
        args.putString("detail", detail);
        fragment.setArguments(args);
        return fragment;
    }


    /**
     * 创建碎片视图
     * @param inflater The LayoutInflater object that can be used to inflate
     * any views in the fragment,
     * @param container If non-null, this is the parent view that the fragment's
     * UI should be attached to.  The fragment should not add the view itself,
     * but this can be used to generate the LayoutParams of the view.
     * @param savedInstanceState If non-null, this fragment is being re-constructed
     * from a previous saved state as given here.
     *
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Toast.makeText(getContext(),"onCreateView:"+getArguments().get("position"),Toast.LENGTH_SHORT).show();
        View view = inflater.inflate(R.layout.fragment_mydynamic, container, false);
        Bundle args = getArguments();
        if (args != null) {
            int position = args.getInt("position");
            int img_id = args.getInt("img_id", R.drawable.android4);
            String name = args.getString("name");
            String detail = args.getString("detail");
            ImageView iv_img = view.findViewById(R.id.iv_img);
            TextView tv_name = view.findViewById(R.id.tv_name);
            TextView tv_detail = view.findViewById(R.id.tv_detail);
            iv_img.setImageResource(img_id);
            tv_name.setText(name);
            tv_detail.setText(detail);
        }
        return view;
    }
    // 页面创建
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(getContext(),"onCreate:"+getArguments().get("position"),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStart() {
        super.onStart();
        Toast.makeText(getContext(),"onStart:"+getArguments().get("position"),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Toast.makeText(getContext(),"onActivityCreated:"+getArguments().get("position"),Toast.LENGTH_SHORT).show();
    }

    // 把碎片贴在页面上
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Toast.makeText(getContext(),"onAttach:"+getArguments().get("position"),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPause() {
        super.onPause();
        Toast.makeText(getContext(),"onPause:"+getArguments().get("position"),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStop() {
        super.onStop();
        Toast.makeText(getContext(),"onStop:"+getArguments().get("position"),Toast.LENGTH_SHORT).show();
    }

    /**
     * 销毁碎片视图
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Toast.makeText(getContext(),"onDestroyView:"+getArguments().get("position"),Toast.LENGTH_SHORT).show();
    }

    /**
     * 页面销毁
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(getContext(),"onDestroy:"+getArguments().get("position"),Toast.LENGTH_SHORT).show();
    }

    /**
     * 把碎片从页面撕下
     */
    @Override
    public void onDetach() {
        super.onDetach();
        Toast.makeText(getContext(),"onDetach:"+getArguments().get("position"),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResume() {
        super.onResume();
        Toast.makeText(getContext(),"onResume:"+getArguments().get("position"),Toast.LENGTH_SHORT).show();
    }
}